<?php


namespace App\Http\Services\FormBuilder\Segments;


use App\Http\Services\FormBuilder\Elements\FormControl;

class BracketButtonWithStockLimit extends FormControl
{
    private   $bracket;
    private   $unit;
    protected $attributes = [
        'class' => 'bracket-with-stock-limit needsclick',
    ];

    public function __construct($bracket)
    {
        $this->bracket = collect($bracket);
    }

    public function render()
    {
        $this->addClass('money-option');
        $this->data('money', $this->bracket->get('money', '0.00'))
            ->data('desc', $this->bracket->get('desc', ''))
            ->data('bracket-id', $this->bracket->get('id', ''));
        $result = '<div';
        $result .= $this->renderAttributes();
        $result .= '>';
        $result .= '<p class="h2 text">' . $this->bracket->get('desc', '') . '</p>';
        $result .= '<p>' . ($this->bracket->get('stock') >= 0 ? '剩余：' . $this->bracket->get('stock', 0) : '数量充足') . '</p>';
        $result .= '<p class="text primary">' . $this->bracket->get('money') . ' ' . $this->unit . '</p>';
        $result .= '</div>';

        return $result;
    }

    public function unit($unit)
    {
        $this->unit = $unit;

        return $this;
    }

}